package com.moco.launcher.activity;

import android.content.Context;
import android.os.Bundle;
import android.provider.Settings.System;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.MotionEvent;
import android.view.inputmethod.InputMethodManager;

import com.moco.launcher.LauncherApplication;
import com.moco.launcher.R;
import com.moco.launcher.wifi.ConnectImpl;
import com.moco.launcher.wifi.NewWifiSettings;
import com.moco.launcher.wifi.WifInputFragment;
import com.moco.launcher.wifi.WifiConnectFragment;
import com.oazon.common.Logger;

public class NewWifiSettingsActivity extends BaseFragmentActivity implements ConnectImpl {
	private static final String TAG = "NewWifiSettingsActivity";
	
	private NewWifiSettings 		mNewWifiSettings;
	private WifiConnectFragment mFragmentWifiConnect;
	private LauncherApplication mApp = null;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.newwifisetting_layout);

		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		if (mNewWifiSettings == null)
			mNewWifiSettings = new NewWifiSettings(this);
		else
			ft.show(mNewWifiSettings);
		if (!mNewWifiSettings.isAdded())
			ft.add(R.id.fragment, mNewWifiSettings, "newWifiSettings").commit();

		mApp = ((LauncherApplication)getApplication());
		mApp.registerActivty(this);
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		mApp.resetStartTime();
		return super.dispatchTouchEvent(event);
	}

	private void hideSoftKeyBoard() {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm.isActive() && getCurrentFocus() != null)
			imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
	}

	@Override
	public void connectNetItem(Bundle accessPoint) {
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction ft = fragmentManager.beginTransaction();
		ft.addToBackStack(null);
		
		if (mFragmentWifiConnect == null)
			mFragmentWifiConnect = new WifiConnectFragment(this, null);
		
		if (!mFragmentWifiConnect.isAdded()) {
			mFragmentWifiConnect.setArguments(accessPoint);
			ft.add(R.id.fragment, mFragmentWifiConnect, "wifiConnectFragment").commit();
		}
	}

	@Override
	public void exit(int type) {
		switch (type) {
		case LIST:
			onBackPressed();
			break;
		case CONNECT:
			String pwd = System.getString(getContentResolver(), INPUT_PWD);
			Logger.i("NewWifiSettings", "exit from connect newWifiSettings is:" + mNewWifiSettings );
			if (mNewWifiSettings != null)
				mNewWifiSettings.connectAp(pwd);
			onBackPressed();
			break;
		case INPUT:
			Logger.i("NewWifiSettings", "exit from input");
			String pwd_ = System.getString(getContentResolver(), INPUT_PWD);
			mFragmentWifiConnect.showInputPwd(pwd_);
			onBackPressed();
			break;
		case FORGET:
			Logger.i("NewWifiSettings", "forget");
			mNewWifiSettings.forgetAp();
			onBackPressed();
			break;
		case FORGET_NOEXIT:
			Logger.i("NewWifiSettings", "FORGET_NOEXIT");
			mNewWifiSettings.forgetAp();
			break;
		case CONNECTBYFIG:
			Logger.i("NewWifiSettings", "CONNECTBYFIG");
			if (mNewWifiSettings != null)
				mNewWifiSettings.connectByConfig();
			onBackPressed();
			break;
		default:
			break;
		}
	}

	WifInputFragment inputFragment;

	@Override
	public void showInputKeyboard() {
		// 先禁止键盘
		hideSoftKeyBoard();
		
		FragmentManager fm = getSupportFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		ft.addToBackStack(null);
		
		if (inputFragment == null)
			inputFragment = new WifInputFragment(this);
		else
			ft.show(inputFragment);
		
		if (!inputFragment.isAdded())
			ft.add(R.id.fragment, inputFragment, "inputFragment").commit();
	}
}
